package controller;

import aop.LogData;
import aop.LogType;
import com.github.pagehelper.PageInfo;
import entity.LayRequest;
import entity.Log;
import entity.MyResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.LogService;

import java.util.*;

@Controller
@RequestMapping("/logs")
public class LogController {
    @Autowired
    private LogService logService;

    @ResponseBody
    @RequestMapping("/insert")
    public MyResult insert(Log log){
        logService.insert(log);
        return new MyResult(0);
    }

    @LogData(logType = LogType.UPDATE,description = "删除日志")
    @ResponseBody
    @RequestMapping("/delete")
    public MyResult deleteByTime(String end){
        if (end==null || end.equals("")){
            return new MyResult(1);
        }
        logService.deleteByTime(end);
        return new MyResult(0);
    }

    @LogData(logType = LogType.DELETE,description = "清空日志")
    @ResponseBody
    @RequestMapping("/delete2")
    public MyResult delete2(){
        logService.deleteAll();
        return new MyResult(0);
    }

    @LogData(logType = LogType.LOGIN,description = "查询日志")
    @ResponseBody
    @RequestMapping("/list")
    public MyResult<List<Log>> list(LayRequest layRequest) {
        PageInfo<Log> logPageInfo = logService.selectByPage(layRequest);
        return new MyResult<>(0,logPageInfo.getTotal(),logPageInfo.getList());
    }
}
